package com.fount4j.generator.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Bean 工具类
 *
 * @author Morven 2016/12/22
 */
public final class BeanUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(BeanUtils.class);

    private BeanUtils() {}

    /**
     * 创建类的实例
     *
     * @param cls 类
     * @param <T> 类的泛型
     * @return 实例对象
     */
    public static <T> T newInstance(Class<T> cls) {
        try {
            return cls.newInstance();
        } catch (InstantiationException e) {
            LOGGER.error("创建实例失败", e);
        } catch (IllegalAccessException e) {
            LOGGER.error("创建实例失败", e);
        }
        return null;
    }

    /**
     * 创建类的实例对象
     *
     * @param cls 类
     * @return 实例对象
     */
    public static Object newInstance(String cls) {
        try {
            return Class.forName(cls).newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            LOGGER.error("创建实例失败", e);
        }
        return null;
    }

}
